package com.example.smart_house;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ToggleButton;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;


public class MainActivity extends AppCompatActivity {

   private Socket client;
     private OutputStream out;
    private EditText ipaddr,port;
    private Button btnLogin;
    private ToggleButton togToilLight,togSecLight,togRestLight,togHallLight,togBoss;
    private ImageView Toilimg,Secimg,Restimg,Hallimg,Bossimg;
    String ctlcmd[] = { "closetoilLight","opentoilLight", //控制命令
                        "closesecFloorLight","opensecFloorLight",
                        "closerestLight","openrestLight",
                        "closehallLight","openhallLight",
                        "closeAll","openAll"
                };
    String tosend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnLogin = (Button) findViewById(R.id.btLogin);
        ipaddr   =(EditText) findViewById(R.id.tbIPaddr);
        port     =(EditText)findViewById(R.id.tbPort);


     

        Toilimg = (ImageView) findViewById(R.id.imgvToil);
        togToilLight = (ToggleButton) findViewById(R.id.togToil);
        togToilLight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toilimg.setImageResource(isChecked ? R.drawable.opentoil : R.drawable.closetoil);
                if(togToilLight.isChecked())
                {
                    tosend = ctlcmd[1]; //开
                }else tosend = ctlcmd[0]; //关

            }
        });

        Secimg = (ImageView) findViewById(R.id.imgvSec);
        togSecLight = (ToggleButton) findViewById(R.id.togSec);
        togSecLight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Secimg.setImageResource(isChecked ? R.drawable.opensec : R.drawable.closesec);
                if(togSecLight.isChecked())
                {
                    tosend = ctlcmd[3]; //开
                }else tosend = ctlcmd[2]; //关
            }
        });

        Restimg = (ImageView) findViewById(R.id.imgvRest);
        togRestLight = (ToggleButton) findViewById(R.id.togRest);
        togRestLight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Restimg.setImageResource(isChecked ? R.drawable.openrest : R.drawable.closerest);
                if(togRestLight.isChecked())
                {
                    tosend = ctlcmd[5]; //开
                }else tosend = ctlcmd[4]; //关
            }
        });

        Hallimg = (ImageView) findViewById(R.id.imgvHall);
        togHallLight = (ToggleButton) findViewById(R.id.togHall);
        togHallLight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Hallimg.setImageResource(isChecked ? R.drawable.openhall : R.drawable.closehall);
                if(togHallLight.isChecked())
                {
                    tosend = ctlcmd[7]; //开
                }else tosend = ctlcmd[6]; //关
            }
        });

        Bossimg = (ImageView) findViewById(R.id.imgvBoss);
        togBoss = (ToggleButton) findViewById(R.id.togBoss);
        togBoss.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Bossimg.setImageResource(isChecked ? R.drawable.openall : R.drawable.closeall);
                if(togBoss.isChecked())
                {
                    tosend = ctlcmd[9]; //开
                }else
                    tosend = ctlcmd[8]; //关
            }
        });
    }

    public  void Sendinfo()
    {
        if(tosend.equals(ctlcmd[0])) {
            try {
                out.write(ctlcmd[0].getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(tosend.equals(ctlcmd[1])) {
            try {
                out.write(ctlcmd[1].getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(tosend.equals(ctlcmd[2])) {
            try {
                out.write(ctlcmd[2].getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(tosend.equals(ctlcmd[3])) {
            try {
                out.write(ctlcmd[3].getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(tosend.equals(ctlcmd[4])) {
            try {
                out.write(ctlcmd[4].getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(tosend.equals(ctlcmd[5])) {
            try {
                out.write(ctlcmd[5].getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(tosend.equals(ctlcmd[6])) {
            try {
                out.write(ctlcmd[6].getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(tosend.equals(ctlcmd[7])) {
            try {
                out.write(ctlcmd[7].getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(tosend.equals(ctlcmd[8])) {
            try {
                out.write(ctlcmd[8].getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(tosend.equals(ctlcmd[9])) {
            try {
                out.write(ctlcmd[9].getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void Login(View v)
    {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    SockClient(ipaddr.getText().toString(),new Integer(port.getText().toString()));
                }
            }).start();
    }

    public  void hallctl(View v)
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Sendinfo();
            }
        }).start();
    }


    public void SockClient(String ip,int port) {
        try {
           client = new Socket(ip, port);
            out = client.getOutputStream();

            String msg ="Login ok!";
                out.write(msg.getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}